package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.watson.R;
import com.watson.utils.StringUtils;

import java.util.ArrayList;
import java.util.Map;


public class LianAdapter extends RecyclerView.Adapter<LianAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private ArrayList<Map> mList;
    private String str = "";
    private ArrayList<Map> codeList = new ArrayList<>();

    public LianAdapter(Context mContext, ArrayList<Map> mList, ArrayList<Map> codeList) {
        this.mContext = mContext;
        this.mList = mList;
        this.codeList = codeList;
    }

    public void setData(ArrayList<Map> vList) {
        this.mList = vList;
        str = "";
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.itme_lian, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            String batch = StringUtils.isEmpty(mList.get(position).get("batch") + "") ? "" : mList.get(position).get("batch") + "";
            holder.tvpc.setText(batch);
            int num = 0;
            if (codeList.size() > 0) {
                for (int i = 0; i < codeList.size(); i++) {
                    if (mList.get(position).get("batchmotorids").toString().trim().contains(codeList.get(i).get("motorid").toString().trim())) {
                        num++;
                    }
                }
            }
            if (batch.equals(str)) {
                holder.tvpc.setVisibility(View.INVISIBLE);
            } else {
                str = batch;
                holder.tvpc.setVisibility(View.VISIBLE);
            }
            holder.tvstd.setText((StringUtils.isEmpty(mList.get(position).get("storageno") + "") ? "" : mList.get(position).get("storageno") + " --> ")
                    + num + "/" + (StringUtils.isEmpty(mList.get(position).get("usablenum") + "") ? "" : mList.get(position).get("usablenum") + "件") + "");

            holder.itemView.setOnClickListener(v -> {
                if (StringUtils.isFastClick()) {
                    return;
                }
                if (mItemClick != null) {
                    ArrayList<Map> batchmotorids = new ArrayList<>();
                    if (mList.get(position).containsKey("batchmotorids")) {
                        batchmotorids = (ArrayList<Map>) JSON.parseArray(mList.get(position).get("batchmotorids") + "", Map.class);
                        mItemClick.onClick(position, batchmotorids);
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvstd, tvpc;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvstd = itemView.findViewById(R.id.tvstd);
            tvpc = itemView.findViewById(R.id.tvpc);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, ArrayList<Map> mList);
    }
}
